package de.global42.clubapp.model;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;

@Entity
public class ClientCoupon implements Serializable{

   /**
	 * Generated Version UID
	 */
	private static final long serialVersionUID = 2681421252279429629L;

@Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name = "id", updatable = false, nullable = false)
   private Long id = null;
   
   @Version
   @Column(name = "version")
   private int version = 0;
   
   @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
   @NotNull
   protected ClientMission clientMission;
   
   @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
   @NotNull
   protected Coupon coupon;
   
   @Temporal(TemporalType.TIMESTAMP)
   protected Date assignedOn;
   
   @Temporal(TemporalType.TIMESTAMP)
   protected Date devaluatedOn;

   public Long getId()
   {
      return this.id;
   }

   public void setId(final Long id)
   {
      this.id = id;
   }

   public int getVersion()
   {
      return this.version;
   }

   public void setVersion(final int version)
   {
      this.version = version;
   }

   public ClientMission getClientMission() {
	return clientMission;
}

public void setClientMission(ClientMission clientMission) {
	this.clientMission = clientMission;
}

public Coupon getCoupon() {
	return coupon;
}

public void setCoupon(Coupon coupon) {
	this.coupon = coupon;
}

public Date getAssignedOn() {
	return assignedOn;
}

public void setAssignedOn(Date assignedOn) {
	this.assignedOn = assignedOn;
}

public Date getDevaluatedOn() {
	return devaluatedOn;
}

public void setDevaluatedOn(Date devaluatedOn) {
	this.devaluatedOn = devaluatedOn;
}

public String toString()
   {
      String result = "";
      if (id != null)
         result += id;
      return result;
   }

   @Override
   public boolean equals(Object that)
   {
      if (this == that)
      {
         return true;
      }
      if (that == null)
      {
         return false;
      }
      if (getClass() != that.getClass())
      {
         return false;
      }
      if (id != null)
      {
         return id.equals(((ClientCoupon) that).id);
      }
      return super.equals(that);
   }

   @Override
   public int hashCode()
   {
      if (id != null)
      {
         return id.hashCode();
      }
      return super.hashCode();
   }

public boolean isStillValid() {
	return getDevaluatedOn() == null;
}

public void devaluate() {
	setDevaluatedOn(new Date());
}
}